import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

public final class ReplaceAll_Str {
    /**
     * 文字分割符，注意分割顺序,分割结果为一行
     */
    public static TreeMap<String,String> split_rules=new TreeMap<>(){{
        // 注意添加顺序
        put("\\x20{3,}", System.lineSeparator());
        // put("\\x20", "");
    }};

    /**
     * 文字替换规则，注意替换顺序
     */
    public static TreeMap<String,String> replace_rules=new TreeMap<>(){{
        // 注意添加顺序
        // put("\\x20{3,}", System.lineSeparator());
        put("\\x20", "");
    }};



    public static List<String> readLines(InputStream inputStream) {
        List<String> lines = new ArrayList<>();
        try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
            while (scanner.hasNextLine()) {
                lines.add(scanner.nextLine());
            }
        }
        return lines;
    }

    public static Stream<String> all_split_by_hashMap( List<String> lines){
            Stream<String> a= lines.stream();
            Set<Entry<String, String>> entrySet = split_rules.entrySet();
            for (Entry<String,String> entry : entrySet) {
                a=a.flatMap(line->Arrays.stream(line.split(entry.getKey())));
            }
            return a;

    }

    public static Stream<String> all_replace_by_hashMap(Stream<String> lines) {
        Stream<String> a = lines;
        Set<Entry<String, String>> entrySet = replace_rules.entrySet();
        for (Entry<String, String> entry : entrySet) {
            // a=a.flatMap(line->Arrays.stream(line.split(entry.getKey())));
            a = a.map(line -> line.replaceAll(entry.getKey(), entry.getValue()));
        }
        return a;
    }

    public static Stream<String> trim_del_kong(Stream<String> lines) {
        Stream<String> a = lines;
        a=a.map(line -> line.trim());
        a=a.filter(line->!line.isEmpty());
        return a;
    }

    public static void write_to_file(Stream<String> lines,File outFile) {
        try {
            // FileUtils.writeLines(lines.collect(Collectors.toList()), outFile);
            FileUtils.writeLines(outFile, StandardCharsets.UTF_8.name(), lines.collect(Collectors.toList()));
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

    public static void write_to_file(List<String> lines,File outFile) {
        try {
            // FileUtils.copyToFile((InputStream) lines, outFile);
            FileUtils.writeLines(outFile, StandardCharsets.UTF_8.name(), lines);
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }



    /**
     * 返回word文档的所有行内容，没有任何修改
     * @param docx_file File类docx_file对象
     * @return 如果成功读取，返回以List String形式的所有段落文本，全部均为未修改
     */
    public static List<String> get_list_string_from_docx(File docx_file,OneF oneF) throws Exception{
        List<String> lines=new ArrayList<>();
        try {
            lines = FileUtils.readLines(oneF.txt_file, StandardCharsets.UTF_8);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            throw e;
        }
        return lines;
        // return lines;
        // try (FileInputStream fis = new FileInputStream(oneF.docx_file)) {
        //     XWPFDocument document = new XWPFDocument(fis);
        //     XWPFWordExtractor ext=new XWPFWordExtractor(document);
        //     String text=ext.getText();
        //     System.out.println(text.substring(0,20));
        //     ext.close();
        //     // 获取所有段落
        //     List<XWPFParagraph> paragraphs = document.getParagraphs();
        //     // String cccc=document.get
            
        //     // 剔除空段
        //     Stream<XWPFParagraph> stream = paragraphs.stream();
        //     Stream<XWPFParagraph> filter = stream.filter(s -> !s.getParagraphText().isEmpty());

        //     List<XWPFParagraph> collect = filter.collect(Collectors.toList());
        //     List<String> all_lines = new ArrayList<>();
        //     // 将所有段落转化成List<String>类型，并且对于不合理的段落进行处理，直到每个题都是空段结尾
        //     for (XWPFParagraph paragraph : collect) {
        //         // String xuhao="";
        //         // if(paragraph.getNumFmt()!=null && paragraph.getNumFmt().equals("decimal")){
        //         //     ;
        //         // }
        //         System.out.println(paragraph.getText());
        //         // System.out.println(paragraph.getStyleID());
        //         String line =paragraph.getParagraphText();
        //         // paragraph.get
        //         all_lines.add(line);
        //         // }
        //     }

        //     // 最终得到word文档里的所有段落
        //     // all_lines：List<String>
        //     document.close();
        //     return all_lines;
        // } catch (IOException e) {
        //             // return new ArrayList<>();
        //     // e.printStackTrace();
        //     throw e;
        //     // return new ArrayList<>();
        // }
        // return new ArrayList<>();
    }
}   
